package com.touchportfolio.view
{
	import com.greensock.TweenLite;
	import com.greensock.easing.*;
	import com.touchportfolio.VO.NodeVO;
	import com.touchportfolio.events.PayloadEvent;
	import com.touchportfolio.loadQueue.LoadQueue;
	import com.touchportfolio.loadQueue.loaders.ImageLoader;
	import com.touchportfolio.model.ApplicationModel;
	import com.touchportfolio.model.SettingsModel;
	import com.touchportfolio.model.utils.EffectsUtilities;
	
	import flash.display.Sprite;
	import flash.events.Event;

	public class Client extends RowBasic
	{	
		private var _scale:Number;
		private var _bg:Sprite;
		private var _content:Sprite;
		private var _isActive:Boolean;
		private var _clientInfo:ClientInfo;
		private var imageLoaded:ImageLoader;
		
		public function Client(node:NodeVO)
		{
			_node = node;
			
			_scale = nodeOpenHeight / nodeActiveHeight;
			
			drawView();
			
			_appModel = ApplicationModel.getInstance();
			_appModel.addEventListener(ApplicationModel.ACTIVE_ROW_CHANGE, onActiveRowChange);
			_appModel.addEventListener(ApplicationModel.ACTIVE_ROW_OPEN, onActiveRowOpen);
			_appModel.addEventListener(ApplicationModel.ACTIVE_ROW_CLOSE, onActiveRowClose);
			_appModel.addEventListener(ApplicationModel.COLOR_CHANGE, colorChange);
			_appModel.stage.addEventListener(Event.RESIZE, onStageResize);
		}
		
		private function drawView():void
		{
			_bg = new Sprite();
			_bg.graphics.beginFill(0xFFFFFF);
			_bg.graphics.drawRect(0, 0, 2000, nodeActiveHeight);
			_bg.graphics.endFill();
			addChild(_bg);
			
			
			// zet alles van de small view op _content
			_content = new Sprite();
			addChild(_content);
			
			
			var q:LoadQueue = LoadQueue.getInstance();
			
			imageLoaded = new ImageLoader("http://ultimedia.biz/touch/logo/logo.png");
			imageLoaded.addEventListener(PayloadEvent.PAYLOAD, onImageLoaded);
			q.addTask(imageLoaded);
			//imageLoaded.y = 25;
			_content.addChild(imageLoaded);
			
			///
			
			
			_slideshowCloseBtn = new CloseButton();
			_slideshowCloseBtn.y = 25;
		}
		
		private function showFullView():void
		{
			_bg.scaleX = _bg.scaleY = _scale;
			
			//// toon full info als row open is
			
			
			/////
			
			_slideshowCloseBtn.x = _appModel.stage.stageWidth - _slideshowCloseBtn.width - 20;
			//_content.addChild(_slideshowCloseBtn);
			
			_appModel.slideshowReady = true;
			
		}
		

		
		private function onActiveRowChange(e:Event):void
		{
			if(_appModel.activeRow == _node.rowId)
			{
				TweenLite.to(_bg,.6,{height:nodeActiveHeight, ease:Elastic.easeOut});
				
				//EffectsUtilities.setSpriteToColorTween(this,ApplicationModel.getInstance().color,0.2,false,1);
				EffectsUtilities.setSpriteToColor(this, ApplicationModel.getInstance().color, 0);
				_isActive = true;
				imageLoaded.y = 25;
			}
			else if(_isActive)
			{
				TweenLite.to(_bg,1,{height:nodeHeight, ease:Elastic.easeOut, onComplete: function():void {
					// dirty hack
					TweenLite.to(_bg, .3, {height:nodeActiveHeight, ease:Elastic.easeOut});
				}});
				imageLoaded.y =0;
				EffectsUtilities.setSpriteToColor(this, ApplicationModel.getInstance().color, 1);
				_isActive = false;
			}
		}
		
		private function colorChange(e:Event):void{
			if(!_isActive){
				EffectsUtilities.setSpriteToColor(this, ApplicationModel.getInstance().color, 1);
			}
		}
		
		private function onActiveRowOpen(e:Event):void
		{
			if((_appModel.activeRow == _node.rowId) && !_appModel.slideshowReady)
			{
				TweenLite.to(_bg,.5,{scaleY:_scale, ease:Elastic.easeOut, onComplete:showFullView});
				
				_appModel.activeImage = 0;
				_appModel.totalImages = 0;
				
				_clientInfo = new ClientInfo();
				_clientInfo.aboutText.text = _node.about;
				_content.addChild(_clientInfo);
				this.addChild(imageLoaded);
				
			}
		}
		
		private function onImageLoaded(e:Event):void
		{
			
		}
		
		private function onActiveRowClose(e:Event):void
		{
			if(_appModel.activeRow == _node.rowId)
			{
				// verwijder full info view elementen
				if(_content.contains(_slideshowCloseBtn)) _content.removeChild(_slideshowCloseBtn);
				if(_clientInfo != null){
					if(_content.contains(_clientInfo)) _content.removeChild(_clientInfo);
				}
				
				TweenLite.to(_bg,.6,{scaleY:1, ease:Elastic.easeOut, onComplete:function():void{_appModel.slideshowReady = false;}});
			}
		}
		
		private function onStageResize(e:Event):void
		{
			_slideshowCloseBtn.x = _appModel.stage.stageWidth - _slideshowCloseBtn.width - 20;
		}
	}
}